package code;

import gui.BoardPanel;
import gui.ImageUtils;

import java.awt.Image;
import java.io.IOException;

import javax.swing.JFrame;


public class Board
{
	private Cell[][] board;
	private int rows;
	private int columns;
	private Player player;
	
	public Board(int rows, int columns, Player player)
	{
		board = new Cell[rows][columns];
		
		this.rows = rows;
		this.columns = columns;	
		this.player = player;
	}
	
	public void setCell(int row, int column, Cell cell)
	{
		board[row][column] = cell;
	}
	
	public void GENERATEBOARD()
	{
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++ )
			{
				if (i > 10 && j > 10)
					board[i][j] = new WaterCell("~");
				else
					board[i][j] = new Ground("_");
			}
		}
		
		this.player = new Player("P");
		((ContainerCell)board[3][3]).placeObject(player);
		
		board[0][7] = new GoalCell("_", player);
		board[0][0] = new SwitchCell("#", (GoalCell)board[0][7]);
		
		
		((ContainerCell)board[5][5]).placeObject(new Crate("C"));
		((ContainerCell)board[1][2]).placeObject(new Crate("C"));
		((ContainerCell)board[3][7]).placeObject(new Crate("C"));
		((ContainerCell)board[6][8]).placeObject(new IceCube("H"));
		
		prepareTiles();
	}
	
	public void prepareTiles()
	{	
		TreeCell aux = new TreeCell("");
		
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++ )
			{
				if (i > 0)
					board[i][j].setTop(board[i-1][j]);
				else
					board[i][j].setTop(aux);
				
				if (i  < rows -1)
					board[i][j].setBottom(board[i+1][j]);
				else
					board[i][j].setBottom(aux);
				
				if (j  < columns -1)
					board[i][j].setRight(board[i][j+1]);
				else
					board[i][j].setRight(aux);
				
				if (j > 0)
					board[i][j].setLeft(board[i][j-1]);
				else
					board[i][j].setLeft(aux);			
			}
		}
	}
	
	public void PRINT()
	{
		for (int i = 0; i < rows; i++)
		{
			for(int j = 0; j < columns; j++)
			{
				System.out.print(board[i][j].PRINT());
			}
		System.out.println();
		}		
	}
	
	public void movePlayer(Direction dir)
	{
		player.attemptMove(dir);
	}
	public static void main(String[] args) throws IOException
	{
		JFrame demo = new JFrame("SilverSphere");
		
		BoardPanel b = new BoardPanel(30, 30, 30);
		Image img = ImageUtils.loadImage("resources/box.png");
		Image img2 = ImageUtils.loadImage("resources/player.png");
		Image img3 = ImageUtils.loadImage("resources/cell.png");
		Image img4 = ImageUtils.loadImage("resources/tree.png");
		Image img5 = ImageUtils.loadImage("resources/water.png");
		for(int i = 0; i < 30; i++){
			for(int j = 0; j < 30; j++){
				if(i < 20 && j < 25)
					b.setImage(i, j, img3);
				else
					b.setImage(i, j, img5);
			}
		}
		b.appendImage(15, 15, img);
		b.appendImage(15, 17, img2);
		b.appendImage(16, 17, img4);
		b.appendImage(16, 20, img4);
		b.appendImage(15, 12, img4);
		b.appendImage(14, 15, img4);
		b.setVisible(true);
		demo.setVisible(true);
		demo.add(b);
		
		
		
		
	}
	
}
